#!/bin/bash
user=$(whoami)
piplist=$(pip3 list 2> /dev/null)
function version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
function ubuntuDenpendencicyCheck(){
	if [ -z "$(dpkg -l $1 | grep $1 | grep ii)" ] ; then
		echo -e "\e[31m$1:not installed\e[0m"
	else
		echo -e "\e[32m$1:\e[0m$(dpkg -l $1| grep $1| grep ii)"
	fi
}
function centosDenpendencicyCheck(){
	if [ -z "$(rpm -qa | grep $1)" ] ; then
		echo -e "\e[31m$1:not installed\e[0m"
	else 
		echo -e "\e[32m$1:$(rpm -qa | grep $1)\e[0m"
	fi
}
function dependenciesCheck(){
	echo "System environment and dependencies"
	echo -e "\e[32mSystem:\e[0m$(uname -a)" 
	if [ -z "$(gcc --version 2> /dev/null | awk 'NR==1')" ] ; then
		echo -e "\e[31mgcc:not installed\e[0m"
	elif  version_ge $(gcc -v 2>&1 | awk 'END{print $3}') "7.3.0" ; then
		echo -e "\e[32mgcc:\e[0m$(gcc --version 2> /dev/null | awk 'NR==1')"
	else 
		echo -e "\e[31mgcc:$(gcc -v 2>&1 | awk 'END{print $3}')\e[26Grequired version >= 7.3.0\e[0m"
	fi
	if [ -z "$(g++ --version 2> /dev/null | awk 'NR==1')" ] ; then 
		echo -e "\e[31mg++:not installed\e[0m"
	else
		echo -e "\e[32mg++:\e[0m$(g++ --version 2> /dev/null | awk 'NR==1')"
	fi
	if [ -z "$(make --version 2> /dev/null | awk 'NR==1')" ] ; then 
		echo -e "\e[31mmake:not installed\e[0m"
	else
		echo -e "\e[32mmake:\e[0m$(make --version 2> /dev/null | awk 'NR==1')"
	fi
	#echo -e "\e[32mg++:\e[0m$(g++ --version 2> /dev/null | awk 'NR==1')"
	#echo -e "\e[32mmake:\e[0m$(make --version 2> /dev/null | awk 'NR==1')"
	if [ -z "$(cmake --version 2> /dev/null | awk 'NR==1')" ] ; then
		echo -e "\e[31mcmake:not installed\e[0m"
	elif version_ge $(cmake --version 2> /dev/null | awk 'NR==1 {print $3}') 3.5.1 ; then
		echo -e "\e[32mcmake:\e[0m$(cmake --version 2> /dev/null | awk 'NR==1')"
	else
		echo -e "\e[31mcmake:$(cmake --version 2> /dev/null | awk 'NR==1 {print $3}')\e[26Grequired version >= 3.5.1\e[0m"
	fi
}
function pipEcho(){
    num=$(echo "$piplist" | grep -c $1)
    ver=$(echo "$piplist" | grep $1)
    if [ -z "$2" ] ; then
        if [ $num == 0 ] ; then 
            echo -e "\e[31m$1\e[26Gnot installed\e[0m"
        fi
        for ((i=1;i<=$num;i++))
        do 
            if [ $(echo "$ver" | awk -v a=$i 'NR==a {print $1}') == $1 ] ; then
                echo -e "\e[32m$1\e[0m\e[26G$(echo "$ver" | awk -v a=$i 'NR==a {print $2}')"   
            fi
        done
    else
        if [ $num == 0 ] ; then 
            echo -e "\e[31m$1\e[26Gnot installed\e[0m"
        fi
        for ((i=1;i<=$num;i++))
        do 
            if [ $(echo "$ver" | awk -v a=$i 'NR==a {print $1}') == $1 ] ; then
                if version_ge $(echo "$ver" | awk -v a=$i 'NR==a {print $2}') $2 ; then
                    echo -e "\e[32m$1\e[0m\e[26G$(echo "$ver" | awk -v a=$i 'NR==a {print $2}')"  
                else  
                    echo -e "\e[31m$1\e[26G$(echo "$ver" | awk -v a=$i 'NR==a {print $2}')\e[52G required version >= $2\e[0m"
                fi
            fi
        done
    fi
}
function GetEnv(){
    #获取driver和firmware版本信息
    device_id=$(npu-smi info | awk 'NR==7 {print $2}')
    #检查npu-smi 命令是否正常
    if [ ${PIPESTATUS[0]} -ne 0 ] ; then
        echo "ERROR:Please check whether the driver is installed normally and the device is mounted normally"
        exit 1
    fi
    driver_version=$(npu-smi info | awk 'NR==2 {print $5}')
    soc_version=$(npu-smi info | awk 'NR==7 {print $3}')
    
    #获取CANN版本信息
    if [ -z $ASCEND_TOOLKIT_HOME ] ; then
        echo "ERROR:Please source the "set_env.sh" in the CANN installation directory"
        exit 1
    fi
    CANN_root_version=$(cat /usr/local/Ascend/ascend-toolkit/latest/version.cfg 2> /dev/null | awk -F : 'NR==2 {print $2}' | awk -F ] '{print $1}') 
    CANN_user_version=$(cat $HOME/Ascend/ascend-toolkit/latest/version.cfg 2> /dev/null | awk -F : 'NR==2 {print $2}' | awk -F ] '{print $1}')
    CANN_root_file=$(ls -al /usr/local/Ascend/ascend-toolkit/latest 2> /dev/null) 
    CANN_user_file=$(ls -al $HOME/Ascend/ascend-toolkit/latest 2> /dev/null) 
    davinci_device=$(ls -al /dev/davinci* 2> /dev/null) 
    echo -e "\e[32mdavinci_device list in the current environment is \e[0m\n$davinci_device"
    if [[ -z "$CANN_root_version" ]] && [[ -z "$CANN_user_version" ]] ; then
        echo "ERROR:can't find CANN in the default installation location,please check CANN installed"
        exit 1
    elif [[ $user == "root" ]] && [[ $ASCEND_TOOLKIT_HOME == "/usr/local/Ascend/ascend-toolkit/latest" ]] ; then
        CANN_current_version=$CANN_root_version
        echo -e "\e[32mYour current CANN version is $CANN_current_version,installation location is $ASCEND_TOOLKIT_HOME\e[0m"
        echo -e "\e[32mAll folders in the current CANN directory have \e[0m\n$CANN_root_file"
    elif [[ $user != "root" ]] && [[ $ASCEND_TOOLKIT_HOME == "$HOME/Ascend/ascend-toolkit/latest" ]] ; then
        CANN_current_version=$CANN_user_version
        echo -e "\e[32mYour current CANN version is $CANN_current_version,installation location is $ASCEND_TOOLKIT_HOME\e[0m"
        echo -e "\e[32mAll folders in the current CANN directory have \e[0m\n$CANN_user_file"
    else
        echo "ERROR:The CANN you are using does not match the installation user"
    fi


    echo "CANN version:"
    echo -e "\e[32mCANN_root_version:\e[0m$CANN_root_version"
    echo -e "\e[32mCANN_user_version:\e[0m$CANN_user_version"
    echo -e "\e[32mCANN_current_version:\e[0m$CANN_current_version"
    echo -e "\e[32mdriver_version:\e[0m$driver_version"
    echo -e "\e[32msoc_version:\e[0m$soc_version"
    tag1=0
    if [ $user == "root" ] ; then
        firmware_version=$(npu-smi info -t board -i $device_id | awk 'NR==7 {print $4}')
    else 
        tag1=1
    fi
         
    echo -e "\e[32mfirmware_version:\e[0m$firmware_version"
    if [ "$tag1" == "1" ]; then
        echo -e "ERROR:Please use \e[32mroot\e[0m user run the commmand \"\e[32m""npu-smi info -t board -i device_id | awk 'NR==7 {print \$4}'""\e[0m\" to get firmware version"
    fi   
    if [ -z "$firmware_version" ] ; then
        echo -e "\e[33mWarning:if the machine is VM or running user is not root, the firmware_version is null\e[0m"
    fi
    echo "-----------------------------------------------------------------"
    echo "Generic settings environment variables:"
    echo -e "\e[32mLD_LIBRARY_PATH:\e[0m$LD_LIBRARY_PATH"
    echo -e "\e[32mPATH:\e[0m$PATH"
    echo -e "\e[32mASCEND_TOOLKIT_HOME:\e[0m$ASCEND_TOOLKIT_HOME"
    echo -e "\e[32mPYTHONPATH:\e[0m$PYTHONPATH"
    echo -e "\e[32mASCEND_AICPU_PATH:\e[0m$ASCEND_AICPU_PATH"
    echo -e "\e[32mASCEND_OPP_PATH:\e[0m$ASCEND_OPP_PATH"
    echo -e "\e[32mTOOLCHAIN_HOME:\e[0m$TOOLCHAIN_HOME"
    echo -e "\e[32mASCEND_HOME_PATH:\e[0m$ASCEND_HOME_PATH"
    echo "----------------------------------------------------------------"
    echo "Samples environment variables:"
    echo -e "\e[32mCPU_ARCH:\e[0m$CPU_ARCH"
    echo -e "\e[32mTHIRDPART_PATH:\e[0m$THIRDPART_PATH"
    echo -e "\e[32mINSTALL_DIR:\e[0m$INSTALL_DIR"
    echo -e "\e[32mDDK_PATH:\e[0m$DDK_PATH"
    echo -e "\e[32mNPU_HOST_LIB:\e[0m$NPU_HOST_LIB"
    echo "----------------------------------------------------------------"
    echo "python and pip version"
    echo -e "\e[32mpython3:\e[0m$(python3 -V)"
    echo -e "\e[32mpip3:\e[0m$(pip3 -V)"
    echo "----------------------------------------------------------------"
    echo "pip dependencis"
    pipEcho 'numpy' 1.14.3
    pipEcho 'decorator' 4.4.0
    pipEcho 'sympy' 1.4
    pipEcho 'cffi' 1.12.3
    pipEcho 'protobuf' 3.11.3
    pipEcho 'attrs'
    pipEcho 'PyYAML'
    pipEcho 'pathlib2'
    pipEcho 'scipy'
    pipEcho 'requests'
    pipEcho 'psutil'
    pipEcho 'absl-py'
    echo "----------------------------------------------------------------"
    system=$(hostnamectl | grep "Operating System" | awk '{print $3}')
	if [ "$system" == "Ubuntu" ] || [ "$system" == "Debian" ] || [ "$system" == "UOS20" ] || [ "$system" == "UOS20 SP1" ] || [ "$system" == "Linx" ] ; then
		dependenciesCheck
		ubuntuDenpendencicyCheck zlib1g
		ubuntuDenpendencicyCheck zlib1g-dev
		ubuntuDenpendencicyCheck libsqlite3-dev
		ubuntuDenpendencicyCheck openssl
		ubuntuDenpendencicyCheck libssl-dev
		ubuntuDenpendencicyCheck libffi-dev
		ubuntuDenpendencicyCheck unzip
		ubuntuDenpendencicyCheck pciutils
		ubuntuDenpendencicyCheck net-tools
		ubuntuDenpendencicyCheck libblas-dev
		ubuntuDenpendencicyCheck gfortran
		ubuntuDenpendencicyCheck libblas3

	elif [ "$system" == "CentOS" ]  || [ "$system" == "EulerOS" ] || [ "$system" == "openEuler" ] || [ "$system" == "BCLinux" ] || [ "$system" == "Kylin" ] || [ "$system" == "UOS20 1020e" ] ; then
		dependenciesCheck
		centosDenpendencicyCheck unzip
		centosDenpendencicyCheck zlib-devel
		centosDenpendencicyCheck libffi-devel
		centosDenpendencicyCheck openssl-devel
		centosDenpendencicyCheck pciutils
		centosDenpendencicyCheck net-tools
		centosDenpendencicyCheck sqlite-devel
		centosDenpendencicyCheck lapack-devel
		centosDenpendencicyCheck gcc-gfortran
	else
        pipEcho "gnureadline"
		centosDenpendencicyCheck gcc
		centosDenpendencicyCheck make
		centosDenpendencicyCheck unzip
		centosDenpendencicyCheck zlib-devel
		centosDenpendencicyCheck openssl-devel
		centosDenpendencicyCheck pciutils
		centosDenpendencicyCheck net-tools
		centosDenpendencicyCheck gdbm-devel
		centosDenpendencicyCheck libffi-devel
	fi
    echo "----------------------------------------------------------------"
    echo "samples dependencies"
    if [ -z $(pkg-config --modversion opencv 2> /dev/null) ] ; then
        echo -e "\e[31mopencv:not installed\e[0m"
    else
        echo -e "\e[32mopencv:\e[0m$(pkg-config --modversion opencv 2> /dev/null)"
    fi
    if [ -z $(find /usr/local/lib -name "libx264.so") ] ; then
        echo -e "\e[31mx264:not installed\e[0m"
    else 
        echo -e "\e[32mx264:\e[0minstalled"
    fi
    if [ "ffmpeg" != "$(echo "$(ffmpeg -version 2> /dev/null)" | awk 'NR==1 {print $1}')" ] ; then
        echo -e "\e[31mffmpeg:not installed\e[0m"
    else 
        echo -e "\e[32mffmpeg:\e[0m$(echo "$(ffmpeg -version)" | awk 'NR==1 {print $3}')"
    fi
    if [ -z $(find $THIRDPART_PATH/lib -name "libacllite.so") ] ; then
        echo -e "\e[31maclliteCPP:not installed\e[0m"
    else 
        echo -e "\e[32maclliteCPP:\e[0minstalled"
    fi
    pipEcho 'Cython'
    pipEcho 'av'
    pipEcho 'Pillow'
}
GetEnv
